﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using SynoManager.GUI;

namespace SynoManager.DownloadStation.Plugins
{
    [DownloadStationPlugin("Queue Manager", "Manage the number of active downloads", "Grad van Horck", "1.0")]
    public class QueueManager : IDownloadTaskPlugin
    {
        private DownloadStationService service;
        private bool resumeAllOnClose = Properties.Settings.Default.ResumeAllOnClose;
        private bool pauseAllOnStart = Properties.Settings.Default.PauseAllOnStart;

        public void OnTaskAdded(DownloadTask task)
        {
        }

        public void OnTaskUpdate(DownloadTask task)
        {
        }

        public void OnTaskStateChange(DownloadTask task)
        {
        }

        public void OnTaskRemoved(DownloadTask task)
        {
        }


        public void Initialize(DownloadStationService service)
        {
            this.service = service;

            service.TasksUpdated += new EventHandler(service_TasksUpdated);
        }

        void service_TasksUpdated(object sender, EventArgs e)
        {
            if (pauseAllOnStart)
            {
                service.PauseAll();
                service.TasksUpdated -= new EventHandler(service_TasksUpdated);
                return;
            }
        }

        public void Close()
        {
            if (ResumeAllOnClose)
                service.ResumeAll();
        }

        public bool ResumeAllOnClose
        {
            get { return resumeAllOnClose; }
            set
            {
                resumeAllOnClose = value;
                Properties.Settings.Default.ResumeAllOnClose = value;
            }
        }

        public bool PauseAllOnStart
        {
            get { return pauseAllOnStart; }
            set 
            { 
                pauseAllOnStart = value;
                Properties.Settings.Default.PauseAllOnStart = value;
            }
        }
    }
}
